package aiku.numericalcontroloa.project.system.controller;

import aiku.numericalcontroloa.common.base.BaseController;
import aiku.numericalcontroloa.common.base.Result;
import aiku.numericalcontroloa.project.system.pojo.entity.SysMenu;
import aiku.numericalcontroloa.project.system.pojo.query.SysMenuParams;
import aiku.numericalcontroloa.project.system.pojo.query.SysRoleParams;
import aiku.numericalcontroloa.project.system.service.SysMenuService;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/sysMenu")
public class SysMenuController extends BaseController {

    @Resource
    SysMenuService sysMenuService;


    /**
     * 获取当前用户具备的前端路由
     *
     * @return
     */
    @GetMapping("/menuRoute")
    public Result initMenu() {
//        Integer uid = RequestContext.getUserId();
//        String uid = "10000";
        String uid = StpUtil.getLoginId().toString();

        return success(sysMenuService.menuTree(uid));
    }

    /**
     * 获取所有菜单列表
     *
     * @return
     */
    @GetMapping("/menuList")
    public Result menuList() {
        List<SysMenu> menuList = sysMenuService.allMenuTree();
        return success(menuList,"获取菜单信息成功");
    }

    /**
     * 新增菜单
     *
     * @return
     */
    @PostMapping("/insert")
    public Result insertMenu(@RequestBody SysMenu menu) {
        return success(sysMenuService.insert(menu));
    }

    /**
     * 删除
     *
     * @return
     */
    @PostMapping("/delete")
    public Result deleteMenu(@RequestBody SysMenuParams params) {
        if (sysMenuService.deleteByPrimaryKey(params) == 0) {
            return error("当前菜单存在子菜单，删除失败");
        }
        return success("删除成功");
    }

    /**
     * 查找子级
     *
     * @return
     */
    @PostMapping("/selectChild")
    public Result selectChild(@RequestBody SysMenuParams query) {
        return success(sysMenuService.selectChildByPrimaryKey(query));
    }

    /**
     * 修改
     *
     * @return
     */
    @PostMapping("/update")
    public Result updateMenu(@RequestBody SysMenu menu) {
        return success(sysMenuService.updateByPrimaryKey(menu));
    }

    /**
     * 查找所有菜单
     *
     * @return
     */
    @GetMapping("/onlyAllMenu")
    public Result onlyAllMenu() {
        List<SysMenu> menuList = sysMenuService.onlyAllMenuTree();
        return success(menuList);
    }

    /**
     * 根据角色查找菜单
     *
     * @return
     */
    @GetMapping("/listByRoleId")
    public Result listByRoleId(SysRoleParams query) {
        List<SysMenu> menuList = sysMenuService.listByRoleId(query);
        return success(menuList);
    }
}
